/**
 * <p>Package : org.tea.springdata.mongdb.dao</p>
 * <p>E-Mail : ztlyde@gmail.com</p>
 */
package org.tea.springdata.mongdb.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.tea.springdata.mongdb.dao.IAccountDao;
import org.tea.springdata.mongdb.domain.Account;
import org.tea.springdata.mongdb.domain.Person;

/**
 * 账户DAO
 * @author: John zhang
 * @version: 0.1
 */
@Repository
public class AccountDao implements IAccountDao {

	@Autowired
	MongoOperations mongoOperations;
	
	@Autowired
	MongoDbFactory blogDB;
	
	/*
	 * (non-Javadoc)
	 * @param account
	 * @see org.tea.springdata.mongdb.dao.IAccountDao#register(org.tea.springdata.mongdb.domain.Account)
	 */
	@Override
	public void register(Account account) {
		if (!mongoOperations.collectionExists(Account.class)) {
			mongoOperations.createCollection(Account.class);
		}
		Query q = new Query(Criteria.where("username").is(account.getUsername()));
		Account entity = mongoOperations.findOne(q, Account.class);
		if(entity != null) {
			System.out.println("当前账户已经存在,请重输...");
			return;
		}
		mongoOperations.insert(account);
		// TODO 在注册时可对密码进行加密处理
		System.out.println("注册成功!");
	}
	
	/*
	 * (non-Javadoc)
	 * @param account
	 * @return
	 * @see org.tea.springdata.mongdb.dao.IAccountDao#login(org.tea.springdata.mongdb.domain.Account)
	 */
	@Override
	public boolean login(Account account) {
		Query q = new Query(Criteria.where("username").is(account.getUsername()).and("password").is(account.getPassword()));
		Account entity = mongoOperations.findOne(q, Account.class);
		if(entity == null) {
			System.out.println("用户名或密码错误");
			return false;
		} else {
			entity.setLastLoginTime(new Date().getTime());
			mongoOperations.save(entity);
			System.out.println(String.format("登录成功! id=%s", entity.getId()));
			return true;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @return
	 * @see org.tea.springdata.mongdb.dao.IAccountDao#findAll()
	 */
	@Override
	public List<Account> findAll() {
		return mongoOperations.findAll(Account.class);
	}
	
	/*
	 * (non-Javadoc)
	 * @param account
	 * @see org.tea.springdata.mongdb.dao.IAccountDao#delete(org.tea.springdata.mongdb.domain.Account)
	 */
	@Override
	public void delete(Account account) {
		
	}
	
	/*
	 * (non-Javadoc)
	 * @param person
	 * @see org.tea.springdata.mongdb.dao.IAccountDao#addInfo(org.tea.springdata.mongdb.domain.Person)
	 */
	@Override
	public void addInfo(Person person) {
		MongoOperations template = new MongoTemplate(blogDB);
		template.insert(person);
		System.out.println("新增基础信息成功");
	}
	
	@Override
	public MongoOperations getTemplate() {
		return mongoOperations;
	}
	
}
